//
//  LYLocalClassViewController.m
//  artist
//
//  Created by Jay_Apple on 16/5/11.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYLocalClassViewController.h"
#import "LYVideoModel.h"
#import "LYEArtistCell.h"
#import "LYCollectListModel.h"
#import "LYClassWebViewController.h"
#import "LYSearchBar.h"
#import "LYScreenBar.h"
#import "LYCityViewController.h"
#import "LYChooseListView.h"


@interface LYLocalClassViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSMutableArray *models;

@property (assign, nonatomic) NSInteger page;

@property (nonatomic, strong) LYSearchBar *searchBar;
@property (weak, nonatomic) LYScreenBar *screenBar;
@property (nonatomic, weak) LYChooseListView *chooseView;

@end

@implementation LYLocalClassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.titleView = self.searchBar;
    
    // 初始化筛选栏
    [self setUpScreenBar];
    
    [self.tableView.mj_header beginRefreshing];
}



#pragma mark - 初始化
/**
 *  初始化筛选栏
 */
- (void)setUpScreenBar{
    
    LYScreenBar *screenBar = [LYScreenBar viewForNib];
    
    screenBar.frame = CGRectMake(0, 0, LYScreenWidth, 40);
    self.screenBar = screenBar;
    
    __weak typeof(self) weakSelf = self;
    __weak typeof(screenBar) weakScreenBar = screenBar;
    [self.view addSubview:screenBar];
    screenBar.block = ^(SceenType type){
        
        switch (type) {
            case SceenTypeSub:
                
                if (weakSelf.chooseView) {
                    [weakSelf.chooseView dismiss];
                }else{
                    
                    weakSelf.chooseView = [LYChooseListView showOnView:weakSelf.tableView];
                    weakSelf.chooseView.block = ^(NSString *title){
                        if (title) {
                            weakScreenBar.subTitle = title;
                            [weakSelf.tableView.mj_header beginRefreshing];
                        }
                        [weakScreenBar backToNormal];
                    };
                }
                break;
                
                
            case SceenTypeEva:
            {
                [weakSelf.chooseView dismiss];
                [self showPickImageAlertView];
            }
                break;
                
            case SceenTypeDistance:
            {
                [weakSelf.chooseView dismiss];
                
                LYCityViewController *cityVC = [[LYCityViewController alloc] init];
                
                cityVC.block = ^(NSString *cityName, NSInteger cityID){
                    
                    weakScreenBar.nearByTitle = cityName;
                    weakScreenBar.area_id = cityID;
                    [weakSelf.tableView.mj_header beginRefreshing];
                };
                
                [self.navigationController pushViewController:cityVC animated:YES];
            }
                break;
                
            default:
                break;
        }
    };
    
}




#pragma mark - 懒加载
- (LYSearchBar *)searchBar{
    
    if (_searchBar == nil) {
        LYSearchBar *searchBar = [[LYSearchBar alloc] init];
        searchBar.frame = CGRectMake(0, 0, CGFLOAT_MAX, 30);
        searchBar.placeholder = @"搜索科目、老师、课程、机构";
        [searchBar addTarget:self action:@selector(textfieldChange:) forControlEvents:UIControlEventEditingChanged];
        
        _searchBar = searchBar;
    }
    return _searchBar;
}
- (void)textfieldChange:(UITextField *)searchBar{
    
    
    [self loadNewData];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/**
 *  请求新数据
 */
- (void)loadNewData{
    
    LYGetSubjectParameterModel *requestModel = [self getRequsetModelWithPage:@"1"];
    
    [LYRequestTool getSubjectListWithParmeterModel:requestModel success:^(NSArray<LYMyClassListModel *> *models) {
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer resetNoMoreData];
        self.models = [models mutableCopy];
        [self.tableView reloadData];
        self.page = 1;
        
        
    } failure:^(NSString *code, NSString *message) {
        [self.tableView.mj_header endRefreshing];
    }];
    
    
}
/**
 *  加载更多数据
 */
- (void)loadMoreData{
    
    NSString *page = [NSString stringWithFormat:@"%zd",self.page + 1];
    
    
    LYGetSubjectParameterModel *requestModel = [self getRequsetModelWithPage:page];
    
    [LYRequestTool getSubjectListWithParmeterModel:requestModel success:^(NSArray<LYMyClassListModel *> *models) {
        
        if (models.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        
        [self.tableView.mj_footer endRefreshing];
        
        [self.models addObjectsFromArray:models];;
        self.page++;
        [self.tableView reloadData];
        
        
    } failure:^(NSString *code, NSString *message) {
        [self.tableView.mj_footer endRefreshing];
    }];
    
}

#pragma mark - 上下拉刷新
/**
 *  下拉
 */
- (void)tableViewHeaderRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadNewData];
    
}

/**
 *  上拉
 */
- (void)tableViewfooterRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadMoreData];
    
}


#pragma mark - 其他

- (LYGetSubjectParameterModel *)getRequsetModelWithPage:(NSString *)page{
    
    LYLocationInfo *locationInfo = [LYDataSourceManager sharedManager].locationInfo;
    
    LYGetSubjectParameterModel *model = [[LYGetSubjectParameterModel alloc] init];
    
    model.order = self.screenBar.order;
    model.area_id = [NSString stringWithFormat:@"%ld",(long)self.screenBar.area_id];
    model.page = page;
    model.per_page = @"10";
    model.token = LYToken;
    model.keyword = self.searchBar.text;
    
    if (locationInfo) {
        model.region = locationInfo.city_id;
    }
    
    if ([model.region isEqualToString:@"-1"] || !model.region) {
        model.region = @"424";
    }
    
    
    return model;
    
}




#pragma mark - tableViewDataSource
/**
 *  cell
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LYEArtistCell *cell = [LYEArtistCell cellWithTableView:tableView andIndetifier:LYEArtistCellIdentifier];
    cell.model = self.models[indexPath.row];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.models.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return LYEArtistCellHeight;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LYMyClassListModel *model = self.models[indexPath.row];
    LYClassWebViewController *classWebVC = [LYClassWebViewController classWebVCWithID:model.Id];
    [self.navigationController pushViewController:classWebVC animated:YES];
    
}



#pragma mark - 私有方法

-(void)showPickImageAlertView {
    
    __weak typeof(self) weakSelf = self;
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *sortAction = [UIAlertAction actionWithTitle:@"智能排序" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [weakSelf alertActionWithTitle:@"智能排序" ordertype:LYLocalOrderTypeDefault];
    }];
    
    UIAlertAction *renqiAction = [UIAlertAction actionWithTitle:@"人气最高" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [weakSelf alertActionWithTitle:@"人气最高" ordertype:LYLocalOrderTypeRenqi];
    }];
    
    UIAlertAction *evaluateAction = [UIAlertAction actionWithTitle:@"评价最多" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [weakSelf alertActionWithTitle:@"评价最多" ordertype:LYLocalOrderTypeEvaluate];
    }];
    
    UIAlertAction *priceAction = [UIAlertAction actionWithTitle:@"价格最低" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [weakSelf alertActionWithTitle:@"价格最低" ordertype:LYLocalOrderTypePrice];
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    }];
    
    
    [alertController addAction:sortAction];
    [alertController addAction:renqiAction];
    [alertController addAction:evaluateAction];
    [alertController addAction:priceAction];
    [alertController addAction:cancelAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}


- (void)alertActionWithTitle:(NSString *)title ordertype:(LYLocalOrderType)order {
    
    self.screenBar.sortTitle = title;
    self.screenBar.order = order;
    [self.tableView.mj_header beginRefreshing];
}

@end
